package com.zhoux.collections.set;

import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author taoyi
 * @version 1.0
 * @date 2021/3/13 10:43
 */
public class Set_Test {
    public static void main(String[] args) {
        UpdateSub stu5 = new UpdateSub("张同学",1005);
        UpdateSub stu1 = new UpdateSub("李同学",1001);
        UpdateSub stu2 = new UpdateSub("周同学",1002);
        UpdateSub stu3 = new UpdateSub("陈同学",1003);
        UpdateSub stu4 = new UpdateSub("王同学",1004);
        TreeSet<UpdateSub> tree = new TreeSet<UpdateSub>();
        tree.add(stu5);
        tree.add(stu4);  // 向集合添加对象
        tree.add(stu2);
        tree.add(stu3);
        tree.add(stu1);
        Iterator<UpdateSub> iterator = tree.iterator(); // 以升序返回 Set 集合中所有对象的迭代器
        System.out.println("Set集合中的所有元素：");
        while (iterator.hasNext()){ // 遍历集合
            UpdateSub sub = (UpdateSub)iterator.next();
            System.out.println(sub.getId()+" "+sub.getName());
        }
        iterator = tree.headSet(stu2).iterator(); // 截取排在stu2对象之前的对象
        System.out.println("截取stu2前面部分的集合：");
        while (iterator.hasNext()){
            UpdateSub sub = (UpdateSub)iterator.next();
            System.out.println(sub.getId()+" "+sub.getName());
        }
        iterator = tree.subSet(stu2,stu4).iterator(); // 截取排在stu2与stu4之间的对象
        System.out.println("截取stu2与stu3之间部分的集合：");
        while (iterator.hasNext()){
            UpdateSub sub = (UpdateSub)iterator.next();
            System.out.println(sub.getId()+" "+sub.getName());
        }
    }
}

